
package com.epam.android.training.app.activities;

import com.epam.android.training.app.R;
import com.epam.android.training.app.db.TestsDb;
import com.epam.android.training.app.db.entities.Tests;
import com.epam.android.training.app.services.TestService;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Test list activity.
 * 
 * @author Natallia_Kukharchyk
 */
public class TestListActivity extends ActivityWithMenu {

  public static final String TEST_ID = "test_id";

  public static final String ONLY_NEW = "onlyNew";

  private ListView testsListView;

  private List<Tests> testsList;

  private TestsDb db;

  private Intent serviceIntent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testlist);
    Intent currentIntent = getIntent();
    boolean onlyNew = currentIntent.getBooleanExtra(ONLY_NEW, false);
    testsList = getTests(onlyNew);
    testsListView = (ListView) findViewById(R.id.test_list);
    testsListView.setAdapter(new TestsArrayAdapter(getApplicationContext(), R.layout.tests_row, testsList));
    testsListView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Tests item = (Tests) a.getAdapter().getItem(position);
        if (null != item) {
          Intent i = new Intent(getApplicationContext(), TestQuestionActivity.class);
          i.putExtra(TEST_ID, item.getTestId());
          startActivity(i);
        }
      }
    });

    serviceIntent = new Intent(getApplicationContext(), TestService.class);
    startService(serviceIntent);

  }

  private List<Tests> getTests(boolean onlyNew) {
    db = new TestsDb(getApplicationContext());
    return db.getTests(onlyNew);
  }

  public class TestsArrayAdapter extends ArrayAdapter<Tests> {
    List<Tests> tests;

    public TestsArrayAdapter(Context context, int textViewResourceId, List<Tests> items) {
      super(context, textViewResourceId, items);
      tests = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.tests_row, null);

        holder = new ViewHolder();
        holder.setTitle((TextView) convertView.findViewById(R.id.test_title_line));
        holder.setDescription((TextView) convertView.findViewById(R.id.description_line));
        holder.setLastScore((TextView) convertView.findViewById(R.id.last_score_line));
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }
      Tests test = tests.get(position);
      if (null != test) {
        holder.getTitle().setText(test.getTitle());
        holder.getDescription().setText(test.getDescription());
        holder.getLastScore().setText(getString(R.string.total_score) + new Long(test.getLastScore()).toString());
        if (test.getLastScore() > 0) {
          holder.getLastScore().setTextColor(Color.rgb(0, 204, 255));
        } else {
          holder.getLastScore().setTextColor(Color.rgb(255, 255, 255));
        }

      }
      return convertView;
    }
  }

  static private class ViewHolder {
    TextView title;

    TextView description;

    TextView lastScore;

    public TextView getTitle() {
      return title;
    }

    public void setTitle(TextView title) {
      this.title = title;
    }

    public TextView getDescription() {
      return description;
    }

    public void setDescription(TextView description) {
      this.description = description;
    }

    public TextView getLastScore() {
      return lastScore;
    }

    public void setLastScore(TextView lastScore) {
      this.lastScore = lastScore;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    stopService(serviceIntent);
    db.close();
  }
}
